package com.demo.controller;

import com.demo.pojo.User;
import com.demo.service.UserService;
import com.demo.utils.GzipUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.zip.GZIPInputStream;

/**
 * @Classname UserController
 * @Description TODO
 * @Date 2021/3/15 22:29
 * @Created by 张斌
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    @GetMapping(path = "/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public List<User> getAllUserInfo() {
        logger.info("查询所有用户信息");
        long startTime = System.currentTimeMillis();
        List<User> list = userService.getAllUserInfo();
        long endTime = System.currentTimeMillis();
        logger.info("接口返回：{},调用耗时：{}", list, (endTime - startTime));

        return list;
    }

    @GetMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public User getUserInfoById(@PathVariable int id) {
        logger.info("查询用户信息,id={}", id);
        long startTime = System.currentTimeMillis();
        User user = userService.getUserInfo(id);
        long endTime = System.currentTimeMillis();
        logger.info("接口返回：{},调用耗时：{}", user.toString(), (endTime - startTime));
        return user;
    }

    @PostMapping(path = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public int addUserInfo(@Valid @RequestBody User user) {
        logger.info("添加用户信息：", user.toString());
        long startTime = System.currentTimeMillis();
        int result = userService.addUser(user);
        long endTime = System.currentTimeMillis();
        logger.info("接口返回：{},调用耗时：{}", result, (endTime - startTime));
        return result;
    }


    /**
     *
     * application/xml格式报文
     * */
    @PostMapping(path = "/test", produces = MediaType.APPLICATION_XML_VALUE, consumes = MediaType.APPLICATION_XML_VALUE)
    public void getUserInfoById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody;
        String resultBody="hello,wolrd";
        byte[] returnByte;
        if (StringUtils.isNoneEmpty(request.getHeader("Content-encoding"))) {
            logger.info("报文压缩，需要进行解压");
            //业务处理
            //返回报文也同样需要进行压缩处理
            assemleResponse(request,response,resultBody);
        }
    }

    public static void assemleResponse(HttpServletRequest request, HttpServletResponse response,String resultBody) throws IOException {
        response.setHeader("Content-Type","application/xml;charset=UTF-8");
        response.setHeader("Content-Encoding","gzip");
        byte[] returnByte=GzipUtil.compress(resultBody);
        OutputStream outputStream=response.getOutputStream();
        outputStream.write(returnByte);
    }
}
